package com.sc.nft.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sc.nft.dao.InformationArticleDao;
import com.sc.nft.entity.InformationArticle;
import com.sc.nft.entity.dto.ArticleCrawlerDTO;
import com.sc.nft.entity.vo.AppArticleListVO;
import com.sc.nft.entity.vo.AppArticleVO;
import com.sc.nft.entity.vo.AppByIdArticleVO;
import com.sc.nft.service.InformationArticleService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class InformationArticleServiceImpl implements InformationArticleService {

    private final InformationArticleDao informationArticleDao;

    private final RedisTemplate redisTemplate;

    /**
     * 文章爬虫
     *
     * @return
     */
    @Override
    public Boolean articleCrawler() {
        JSONObject jsonObject = JSONObject.parseObject(HttpUtil.get("https://www.woshipm.com/tensorflow/digest/list?page=0"), JSONObject.class);
        if (!jsonObject.get("code").equals(0)) {
            return Boolean.FALSE;
        }
        List<ArticleCrawlerDTO> result = JSONObject.parseArray(Convert.toStr(jsonObject.get("result")), ArticleCrawlerDTO.class);
        result.forEach(articleCrawlerDTO -> {
            InformationArticle informationArticle = new InformationArticle();
            informationArticle.setTitle(articleCrawlerDTO.getTitle());
            informationArticle.setContent(articleCrawlerDTO.getContent());
            informationArticle.setAId(articleCrawlerDTO.getId());
            informationArticle.setCreateTime(articleCrawlerDTO.getCreateTime());
            informationArticle.setModifyTime(articleCrawlerDTO.getCreateTime());
            informationArticle.setCreateId(0L);
            informationArticle.setModifyId(0L);
            informationArticle.setIsDelete(false);
            informationArticleDao.selectByAidSaveObject(informationArticle);
        });
        Set keys = redisTemplate.keys("ARTICLE*");
        redisTemplate.delete(keys);
        return Boolean.TRUE;
    }

    @Override
    public List<AppArticleListVO> getArticleList(PageRequest pageRequest) {
        // 组装key
        String format = StrUtil.format("ARTICLE:{}:{}", pageRequest.getPageNo(), pageRequest.getPageSize());
        Boolean dataIsExist = redisTemplate.hasKey(format);
        if (dataIsExist) {
        return JSONObject.parseArray(Convert.toStr(redisTemplate.opsForValue().get(format)),AppArticleListVO.class);
        }
        List<AppArticleListVO> data = new ArrayList<>();
        List<AppArticleVO> articleList = informationArticleDao.getArticleList(pageRequest);
        // 根据时间进行分组
        Map<String, List<AppArticleVO>> collect = articleList.stream().collect(Collectors.groupingBy(AppArticleVO::getDate));
        // 赋值给返回对象
        collect.forEach((date, appArticleVOS) -> {
            AppArticleListVO appArticleList = new AppArticleListVO();
            appArticleList.setDate(Convert.toStr(date));
            appArticleList.setList(appArticleVOS);
            data.add(appArticleList);
        });
        // 把时间进行排序
        List<AppArticleListVO> result = data.stream().sorted(Comparator.comparing(AppArticleListVO::getDate).reversed()).collect(Collectors.toList());
        redisTemplate.opsForValue().set(format,JSON.toJSONString(result));
        return result;
    }

    @Override
    public AppByIdArticleVO getArticleDetails(Long id) {
        AppByIdArticleVO appByIdArticleVO = informationArticleDao.appSelectById(id);
        return appByIdArticleVO;
    }
    /**
     * 获取最新5条,抛出当前的条目
     * 不能使用redis.要抛出非自己的文章.
     * @param id
     * @return
     */
    @Override
    public List<AppByIdArticleVO> getLimitFive(Long id) {
        return informationArticleDao.getLimitFive(id);
    }
}
